home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Interfaces / Universal Interfaces 2.0a3 / CIncludes / stream.h < prev    next >
Encoding:
Text File  |  1994-12-14  |  7.4 KB  |  297 lines  |  [TEXT/MPS ]

  1. //     IOStreams Package
  2. //     Copyright Symantec Corp 1989-1992. All Rights Reserved.
  3. //
  4. //    stream.hpp       Thu Mar  1 1990   Modified by: Walter Bright
  5. //                   Fri Aug 18 1989   Modified by: Steve Teale
  6. //
  7. //
  8. //    Some portions Copyright © 1994 Apple Computer Inc.
  9.  
  10.  
  11. #ifndef __STREAM__
  12. #define __STREAM__
  13.  
  14. extern "C++" {
  15.  
  16. #ifndef __STDIO__
  17. #include    <stdio.h>
  18. #endif
  19.  
  20. #ifndef    BUFSIZE
  21. #define    BUFSIZE    1024
  22. #endif
  23.  
  24. ///////////////////////////// STREAMBUF //////////////////////
  25.  
  26. // Class for dealing with a stream buffer
  27. struct streambuf
  28. {
  29. protected:
  30.     char *base;  // buffer, NULL if no buffer
  31.     char *pptr;  // write pointer (1 past end of data in buffer)
  32.     char *gptr;  // read pointer (next char to read), gptr chases pptr
  33.     char *eptr;  // 1 past end of buffer
  34.     char alloc;        // 1 if base was allocated using new
  35.     char dummy;        // for alignment
  36.     FILE *fp;        // in case streambuf is just a layer over stdio
  37.  
  38.     int doallocate();
  39.     int allocate() { return base ? 0 : doallocate(); }
  40.     streambuf *setbuf(char *buf, int buflen,
  41.             int written = 0, int wasalloc = 0);
  42.  
  43.     friend class ostream;
  44.     friend class istream;
  45.  
  46. public:
  47.  
  48. // Functions for buffer full and empty respectively
  49.     virtual int overflow(int c = EOF);
  50.     virtual int underflow();
  51.  
  52. // Constructors
  53.     streambuf();
  54.     streambuf(char* buf, int buflen);
  55.  
  56. // Destructor
  57.     virtual ~streambuf();
  58.  
  59. // Character by character functions
  60.     int snextc()
  61.     {    return (gptr + 1 < pptr)
  62.         ? (unsigned char) *++gptr
  63.         : underflow();
  64.     }
  65.  
  66.     int sgetc()
  67.     {    return (gptr < pptr)
  68.         ? (unsigned char) *gptr
  69.         : underflow();
  70.     }
  71.  
  72.     void stossc()
  73.     {   (++gptr > pptr) && underflow(); }
  74.  
  75.     int sputc(int c = EOF)
  76.     {
  77.         return fp
  78.             ? putc(c,fp)
  79.             : (pptr < eptr)
  80.                 ? (unsigned char) (*pptr++ = c)
  81.                 : overflow(c);
  82.     }
  83.  
  84.     void sputbackc(char c)
  85.         { (gptr > base) && (*--gptr = c) != 0; }
  86.  
  87. // Access to buffer
  88.     char *bufptr() { return base; }
  89. };
  90.  
  91. /////////////////////// FILEBUF ///////////////////////
  92.  
  93. extern "C" int close(int);        // should match io.h
  94.  
  95. // a stream buffer for files
  96.  
  97. enum open_mode  { input = 0, output = 1, append = 2 };
  98.  
  99. struct filebuf : public streambuf
  100. {
  101.     int fd;
  102.     char opened;
  103.     char dummy;            // for alignment
  104.  
  105. public:
  106.  
  107.     int overflow(int c = EOF);
  108.     int underflow();
  109.  
  110.     filebuf* open(char *name,open_mode om);
  111.     int close();
  112.  
  113.     filebuf();
  114.     filebuf(int _fd);    // file descriptor
  115.     filebuf(FILE* p);
  116.     filebuf(int _fd, char *buf, int buflen);
  117.  
  118.     ~filebuf();
  119. };
  120.  
  121. ///////////////////////// CIRCBUF //////////////////////
  122. // Circular stream buffer
  123.  
  124. struct circbuf : public streambuf
  125. {
  126.     int overflow(int c = EOF);
  127.     int underflow();
  128.  
  129.      circbuf();
  130.     ~circbuf();
  131. };
  132.  
  133. ///////////////////// Input and Output ///////////////////
  134.  
  135. struct whitespace { int dummy; };
  136.  
  137. // State for each istream or ostream
  138. enum state_value
  139. {    _good = 0,    // previous input operation succeeded. state must
  140.             // be _good for subsequent input operations to succeed
  141.     _eof  = 1,    // reached end of file
  142.     _fail = 2,    // error, no characters lost
  143.     _bad  = 4    // the stream is all messed up
  144. };
  145.  
  146. // Output formatting routines
  147.  
  148. extern char *dec(long, int = 0);
  149. extern char *oct(long, int = 0);
  150. extern char *hex(long, int = 0);
  151. extern char *str(const char *, int = 0);
  152. extern char *chr(int, int = 0);
  153. extern char *form(const char * ...);
  154.  
  155.  
  156. ///////////////////////// OSTREAM //////////////////////
  157.  
  158. class ostream
  159. {
  160.     streambuf *bp;
  161.     state_value state;
  162.     int alloc;
  163.  
  164.     friend class istream;
  165.  
  166. public:
  167.  
  168. // Overloads of <<
  169.     ostream& operator<<(streambuf&);
  170.     ostream& operator<<(const whitespace&);
  171.     ostream& operator<<(const char*);
  172.     ostream& operator<<(const signed char *psc)
  173.         { return *this << (const char *) psc; }
  174.     ostream& operator<<(const unsigned char *puc)
  175.         { return *this << (const char *) puc; }
  176.     ostream& operator<<(long);
  177.     ostream& operator<<(unsigned long ul);
  178.     ostream& operator<<(int a) { return *this << (long) a; }
  179.     ostream& operator<<(unsigned a) { return *this << (long) a; }
  180.     ostream& operator<<(char c);
  181.     ostream& operator<<(signed char c) { return *this << (char) c; }
  182.     ostream& operator<<(unsigned char c) { return *this << (char) c; }
  183.     ostream& operator<<(short s) { return *this << (int) s; }
  184.     ostream& operator<<(unsigned short us) { return *this << (unsigned) us; }
  185.     ostream& operator<<(long double);
  186.     ostream& operator<<(const void *);
  187.  
  188. // Other output functions
  189.     ostream& flush()
  190.         { bp->overflow(); return *this; }
  191.     ostream& put(char);
  192.  
  193. // Stream state access functions
  194.     int good() { return state == _good; }
  195.     int eof() { return state & _eof; }
  196.     int fail() { return state & (_fail | _bad); }
  197.     int bad() { return state & _bad; }
  198.  
  199.     operator void *()        { return fail() ? NULL : this; }
  200.     int    operator !()        { return fail(); }
  201.     int rdstate() { return state; }
  202.  
  203. // State set function
  204.     void clear(state_value v = _good) { state = v; }
  205.  
  206. // Access to associated buffer
  207.     char *bufptr() { return bp->bufptr(); }
  208.  
  209. // Constructors
  210.     ostream(streambuf *sb);
  211.     ostream(int fd);
  212.     ostream(int buflen, char *buf);
  213.  
  214. // Destructor
  215.     ~ostream() { flush(); if (alloc) delete bp; }
  216. };
  217.  
  218. ///////////////////////// ISTREAM ///////////////////////////
  219.  
  220. class istream
  221. {
  222.     streambuf *bp;
  223.     ostream *tied_to;
  224.     state_value state;
  225.     char skipws;
  226.     char alloc;
  227.  
  228.     void eatwhite();
  229.  
  230. public:
  231.  
  232. // Overloads of operator>>
  233.     istream& operator>>(streambuf&);
  234.     istream& operator>>(whitespace&);
  235.     istream& operator>>(char&);
  236.     istream& operator>>(char*);
  237.     istream& operator>>(signed char &sc) { return *this >> (char &) sc; }
  238.     istream& operator>>(signed char *p)  { return *this >> (char *) p; }
  239.     istream& operator>>(unsigned char &uc) { return *this >> (char &) uc; }
  240.     istream& operator>>(unsigned char *p)  { return *this >> (char *) p; }
  241.     istream& operator>>(int&);
  242.     istream& operator>>(unsigned &u) { return *this >> (int &) u; }
  243.     istream& operator>>(short &s) { int i ; *this >> i; s = i; return *this; }
  244.     istream& operator>>(unsigned short &us) { int i; *this >> i; us = i; return *this; }
  245.     istream& operator>>(long&);
  246.     istream& operator>>(unsigned long &ul) { return *this >> (long &) ul; }
  247.     istream& operator>>(float&);
  248.     istream& operator>>(double&);
  249.     istream& operator>>(long double&);
  250.  
  251. // Other input functions
  252.     istream& get(char *, int, char = '\n');
  253.     istream& get(streambuf&, char = '\n');
  254.     istream& get(char& c);
  255.     istream& putback(char);
  256.  
  257. // Istream control functions
  258.     ostream* tie(ostream *os);
  259.     int skip(int s);
  260.  
  261. // Stream state access functions
  262.     int good() { return state == _good; }
  263.     int eof() { return state & _eof; }
  264.     int fail() { return state & (_fail | _bad); }
  265.     int bad() { return state & _bad; }
  266.  
  267.     int    operator!()    { return fail(); }
  268.     operator void*()    { return fail() ? 0 : this; }
  269.     int rdstate() { return state; }
  270.  
  271. // Stream state set function
  272.     void clear(state_value v = _good) { state = v; }
  273.  
  274.     char *bufptr()    { return bp->bufptr(); }    
  275.  
  276. // Constructors
  277.     istream(int len, char *string, int s = 1);
  278.     istream(streambuf *sb, int s = 1, ostream *os = NULL);
  279.     istream(int fd, int s = 1, ostream *os = NULL);
  280.  
  281. // Destructor
  282.     ~istream();
  283. };
  284.  
  285.  
  286. /////////////////////
  287. // Predefined I/O streams.
  288. // These are tied to stdin, stdout, stderr, stdprn, stdaux
  289.  
  290. extern istream cin;
  291. extern ostream cout;
  292. extern ostream cerr;
  293.  
  294. } // extern "C++"
  295.  
  296. #endif /* __STREAM__ */
  297.